/*******************************************************************************
  * @file                   shell.c
  * @Author:                MQjehovah                 mail:MQjehovah@hotmail.com
  * @version                1.0.0
  * @date                   2017.11.22
  * @brief                  
  ******************************************************************************
  * @attention
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "shell.h"
#include "ring_buffer.h"
#include "USART.h"
#include "FLASH.h"
#include "delay.h"
#include <stdlib.h>
/* Definition ----------------------------------------------------------------*/
char *CMDList[] = {
    "help",
    "list",
    "iap_down",
    "iap_jump_app",
    "iap_over",
    "iap_set_flag",
    "iap_clear_flag"};
/* Functions -----------------------------------------------------------------*/
/*******************************************************************************
  * @brief  从缓冲区中读取一行指令              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
char *shell_readline(char *buffer)
{
    // char *p = buffer;
    u32 seek = 0;
    USART_SendStr(SHELL_USART, PROMPT);
    while (1)
    {
        if (rx_buffer.lenth <= 0)
            continue;
        // u8 data = RingBuf_read();
        buffer[seek] = RingBuf_read();
        switch (buffer[seek])
        {
        case '\r': /* Enter */
        case '\n': //回车表示命令输入结束
            USART_SendStr(SHELL_USART, "\r\n");
            if (seek == 0)
                return (char *)-1; //空指令不处理
            buffer[seek] = '\0';   //字符串末尾添加结束符
            // USART_SendStr(SHELL_USART, buffer);
            return buffer;
        case '\0': /* nul */
            continue;
        case 0x03: /* ^C - break */
            //CTRL+C表示取消命令输入
            return ((char *)-1);
        case 0x15: /* ^U - erase line */
            continue;
        case 0x17: /* ^W - erase word */
            //CTRL+W表示删除一个词
            continue;
        case 0x08: /* ^H  - backspace */
        case 0x7F: /* DEL - backspace */
            //表示删除一个字符
            if (seek > 0)
            {
                USART_SendStr(SHELL_USART, "\b \b");
                seek--;
            }
            continue;
        default:
            USART_SendChar(SHELL_USART, buffer[seek]);
            seek++;
            break;
        } //switch
    }
}
/*******************************************************************************
  * @brief  指令处理程序              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
int shell_process(char *cmd)
{
    char *argv[2] = {0};
    u8 argc = 0;
    argv[argc++] = cmd;
    // char *cmd = cmdstr;

#ifdef DEBUG
    USART_SendStr(USART1, cmd);
    USART_SendStr(USART1, "\r\n");
#endif
    // long test = atol("0x8000000", (char**)NULL, 16);
    if (cmd == (char *)-1)
        return 0;
    //分词
    while (1)
    {
        if (*cmd == '\0')
            break;
        if (*cmd == ' ')
        {
            *cmd = '\0';
            argv[argc++] = ++cmd;
            continue;
        }
        cmd++;
    }

#ifdef DEBUG
    USART_SendStr(USART1, argv[0]);
#endif

    if (string_compare(argv[0], "help"))
    {
        USART_SendStr(USART1, "Support Command:\r\n");
        USART_SendStr(USART1, "help:\r\n");
        USART_SendStr(USART1, "test:\r\n");
        USART_SendStr(USART1, "read:\r\n");
        USART_SendStr(USART1, "fuck:\r\n");
        return 1;
    }
    if (string_compare(argv[0], "echo"))
    {
        if (argc > 1)
        {
            USART_SendStr(USART1, argv[1]);
            USART_SendStr(USART1, "\r\n");
        }
        return 1;
    }
    if (string_compare(argv[0], "time"))
    {
        printNumber(USART1, sys_Time, 10);
        USART_SendStr(USART1, "\r\n");
        return 1;
    }
    if (string_compare(argv[0], "info"))
    {
        u32 msp = __get_MSP();
        USART_SendStr(USART1, "stack:");
        USART_SendStr(USART1, "0x");
        printNumber(USART1, 0x20005000 - msp, 16);
        USART_SendStr(USART1, "\r\n");
        return 1;
    }
    if (string_compare(argv[0], "read"))
    {
        u32 addr = 0;
        if (argc > 1)
        {
            if (argv[1][0] == '0' && argv[1][1] == 'x')
            {
                addr = strtol(argv[1], (char **)NULL, 16);
            }
        }
        u32 data = FLASH_Readu32(addr);
        printNumber(USART1, data, 16);
        USART_SendStr(USART1, "\r\n");
        return 1;
    }
    if (string_compare(argv[0], "load"))
    {
        USART_SendStr(USART1, argv[1]);
        return 1;
    }
    USART_SendStr(USART1, "Unsupport Command\r\n");
    return 0;
}
/*******************************************************************************
  * @brief  字符串比较            
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
u8 string_compare(char *str1, char *str2)
{
    int seek = 0;
    while (1)
    {
        if (str1[seek] == str2[seek])
        {
            if (str1[seek++] == '\0')
                return 1;
            continue;
        }
        return 0;
    }
}
/*******************************************************************************
  * @brief  下载任务函数      
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
// u8 download()
// {
//     RingBuf_flush();              //先清空缓冲区
//     USART_SendChar(USART1, 0x01); //发送开始传输请求
//     USART_SendChar(USART1, 0x02);
//     USART_SendStr(USART1, "download\r\n");
//     USART_SendChar(USART1, 0x03);
//     u8 buffer[64];
//     u16 seek = 0;
//     enum Rec_State
//     {
//         Rec_Idle,
//         Rec_Header,
//         Rec_Text,
//         Rec_End
//     } state;
//     while (1) //程序下载任务
//     {
//         if (rx_buffer.lenth <= 0)
//             continue;
//         u8 data = RingBuf_read();
//         switch (state)
//         {
//         case Rec_Idle:        //空闲待接收
//             if (data == 0x01) //寻找帧头
//                 state = Rec_Header;
//             break;
//         case Rec_Header: //帧头接收
//             if(data==0x02)
//             break;
//         case 0x03: //正文结束

//             break;
//         default: //数据字符

//             break;
//         }
//     }
// }
/*********************************END OF FILE**********************************/
